/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.web.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.momarketing.entity.OnlineSignup;
import com.boxin.ims.modules.momarketing.entity.OnlineSignupConfig;
import com.boxin.ims.modules.momarketing.service.OnlineSignupConfigService;
import com.boxin.ims.modules.momarketing.service.OnlineSignupService;

/**
 * 在线预约Controller
 * @author Jakemanse
 * @version 2013-09-10
 */
@Controller
@RequestMapping(value = Global.FRONT_PATH+"/mobsite/signup")
public class OnlineSignupFrontController extends BaseController {

	@Autowired
	private OnlineSignupService onlineSignupService;
	
	@Autowired
	SystemService systemService;
	
	@Autowired
	OnlineSignupConfigService onlineSignupConfigService;
	
	
	@RequestMapping(value = {"list", "q"})
	public String list(OnlineSignup onlineSignup, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			onlineSignup.setUser(user);
		}
        Page<OnlineSignup> page = onlineSignupService.find(new Page<OnlineSignup>(request, response), onlineSignup); 
        model.addAttribute("page", page);
		return "modules/momarketing/onlineSignupList";
	}

	@RequestMapping(value = {"reg/{uid}",""})
	public String form(@PathVariable Long uid, OnlineSignup onlineSignup, HttpServletRequest request,HttpServletResponse response, Model model) {
		
		User user = systemService.getUser(uid);
		OnlineSignupConfig config =  onlineSignupConfigService.getByUserId(uid);
		if(config != null){
			onlineSignup.setStartTime(config.getStartTime());
			onlineSignup.setEndTime(config.getEndTime());
		}
		onlineSignup.setUser(user);
		model.addAttribute("onlineSignup", onlineSignup);
		model.addAttribute("config", config);
		model.addAttribute("uid", uid);
		
		return "modules/ims/front/mobile/signup";
	}

	@RequestMapping(value = "save")
	public String save(OnlineSignup onlineSignup, HttpServletRequest request,HttpServletResponse response,Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, onlineSignup)){
			Long uid = Long.parseLong((request.getParameter("uid")));
			return form(uid,onlineSignup, request,response,model);
		}
		onlineSignupService.save(onlineSignup);
		
		OnlineSignupConfig onlineSignupConfig =  onlineSignupConfigService.getByUserId(onlineSignup.getUser().getId());
		model.addAttribute("config", onlineSignupConfig);
		
		addMessage(redirectAttributes, "保存成功");
		return "/modules/ims/front/mobile/signup_ok";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		onlineSignupService.delete(id);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/momarketing/onlineSignup/?repage";
	}

}
